<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目业态" prop="projectBusiness">
            <el-select v-model="queryParams.projectBusiness" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_business" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="已过折旧" prop="isDepreciation">
            <el-select v-model="queryParams.isDepreciation" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
              <el-option v-for="dict in contractFileOptions" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="recordList" border>
      <el-table-column align="center" label="项目区域" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目名称" prop="projectName"/>
      <el-table-column align="center" label="项目业态" prop="projectBusiness">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_business" :value="scope.row.projectBusiness"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="资产名称" prop="assetsName"/>
      <el-table-column align="center" label="资产Id" prop="assetsIdCard"/>
      <el-table-column align="center" label="资产状态" prop="recordStatus">
        <template slot-scope="scope">
          <dict-tag :options="recordStatusOptions" :value="parseInt(scope.row.recordStatus)"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="最近投放时间" prop="placementStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.placementStartTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="最近闲置日期" prop="ldleStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ldleStartTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="最近维修日期" prop="repairTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.repairTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否已过折旧期" prop="isDepreciation">
        <template slot-scope="scope">
          <span>{{ scope.row.isDepreciation == 1 ? '是' : (scope.row.isDepreciation == 2 ? '否' : '是') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="是否新机" align="center" prop="reportDesc" />-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button  size="mini" type="text" @click="handleDeliveryList(scope.row)">投放记录</el-button>
          <el-button v-if="[1].includes(scope.row.recordStatus)" size="mini" type="text" @click="handleInvestUpdate(scope.row)">投放</el-button>
          <el-button v-if="[1, 3].includes(scope.row.recordStatus)"  size="mini" type="text" @click="handleScrapUpdate(scope.row)">报废</el-button>
          <el-button v-if="[2].includes(scope.row.recordStatus)"  size="mini" type="text" @click="handleLedeIdle(scope.row)">闲置</el-button>
          <!--<el-button  size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList"/>

    <!-- 投放资产 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body title="投放资产" width="650px">
      <el-form ref="form" :model="form" label-width="80px">
        <el-row :gutter="20">
          <el-form-item :rules="{
            required: true, message: '负责人不能为空', trigger: 'blur'
          }" label="负责人" prop="staffIds">
            <el-cascader-panel v-model="form.staffIds" :options="projectGroupOptions"></el-cascader-panel>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 报废资产 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="openScrap" append-to-body title="报废资产" width="30%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item :rules="{
          required: true, message: '请输入', trigger: 'blur'
        }" label="报废描述" prop="scrapDesc">
          <el-input v-model="form.scrapDesc" :autosize="{ minRows: 4, maxRows: 6 }" placeholder="请输入"
                    type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="openScrap = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 投放记录 -->
    <el-dialog :title="title" :visible.sync="deliveryOpen" append-to-body width="700px">
      <el-table :data="deliveryList" border>
        <el-table-column align="center" label="项目区域" prop="projectRegion">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目名称" prop="projectName"/>
        <el-table-column align="center" label="投放开始时间" prop="placementStartTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.placementStartTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="投放结束时间" prop="placementEndTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.placementEndTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deliveryOpen = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {addPlacementRecord, deliveryList, delRecord, getRecord, listRecord} from "@/api/cld/record";
import {recordStatusOptions} from '@/assets/staticDict'
import {selectedProjectGroupStaff} from '@/api/cld/publicCommon'

export default {
  name: "Record",
  dicts: ['cld_project_region', 'cld_project_business'],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 资产记录表格数据
      recordList: [],
      // 投放记录
      deliveryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openScrap: false,
      deliveryOpen: false,
      contractFileOptions: [{
        "label": "是",
        "value": 1
      }, {
        "label": "否",
        "value": 2
      }],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isNewUpdate: 1,
        projectRegion: null,
        projectName: null,
        projectBusiness: null,
        isDepreciation: null
      },
      projectGroupOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          {required: true, message: "投放项目不能为空", trigger: "blur"}
        ]
      },
      // 资产状态
      recordStatusOptions: recordStatusOptions,
      // 操作状态 1 闲置 2投放 4 报废
      optionsStatus: null
    };
  },
  created() {
    this.queryParams.assetsId = this.$route.params && this.$route.params.assetsId;
    this.getList();
  },
  methods: {
    /** 查询资产记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        assetsRecordId: null,
        projectId: null,
        staffIds: [],
      };
      this.optionsStatus = null;
      this.staffId = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 投放按钮操作 */
    handleInvestUpdate(row) {
      this.reset();
      const assetsRecordId = row.assetsRecordId || this.ids
      getRecord(assetsRecordId).then(response => {
        this.form = response.data;
        this.optionsStatus = 2;
        this.handleGetProjectGroupAndStaff();
        this.open = true;
      });
    },
    // 查询小组及在岗人员
    handleGetProjectGroupAndStaff() {
      selectedProjectGroupStaff(this.projectId).then(res => {
        this.projectGroupOptions = res.data;
      })
    },
    // 闲置设备
    handleLedeIdle(row) {
      this.$modal.confirm('是否确认闲置该资产？').then(function () {
        let form = {
          assetsRecordId: row.assetsRecordId,
          recordStatus: 1
        }
        return addPlacementRecord(form);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    },
    /** 报废按钮操作 */
    handleScrapUpdate(row) {
      this.reset();
      getRecord(row.assetsRecordId).then(response => {
        this.form = response.data;
        this.form.recordStatus = 4;
        this.openScrap = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.optionsStatus == 2) {
            this.form.projectId = this.form.staffIds[0];
            this.form.personId = this.form.staffIds[1];
            this.form.recordStatus = 2;
          }
          addPlacementRecord(this.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.openScrap = false;
            this.getList();
          });
        }
      });
    },
    // 投放记录
    handleDeliveryList(row) {
      let params = {
        recordStatus: 2,
        assetsIdCard: row.assetsIdCard
      }
      deliveryList(params).then(response => {
        this.deliveryList = response.data;
        this.title = row.assetsName + '-' + row.assetsIdCard + '-投放记录'
        this.deliveryOpen = true;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const assetsRecordIds = row.assetsRecordId || this.ids;
      this.$modal.confirm('是否确认删除资产记录编号为"' + assetsRecordIds + '"的数据项？').then(function () {
        return delRecord(assetsRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
